﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using System.IO;

namespace SilverPOP.Library.Utilities
{
    public static class JsonSerializer
    {
        public static string SerializeObjectIntoJson<T>(List<T> p)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(p.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, p);
                ms.Flush();
                byte[] bytes = ms.GetBuffer();
                string jsonString = Encoding.UTF8.GetString(bytes, 0, bytes.Length).Trim('\0');
                return jsonString;
            }
        }

        public static string SerializeObject<T>(T t)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ser.WriteObject(ms, t);
            string jsonString = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return jsonString;
        }
    }
}
